package com.网络编程.net.http;


import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;


/**
 * 测试发送文件格式数据到接口
 * @author liyiruo
 */
public class HttpSendMultipart {

    public String start(String path,String base64String,String imageFilePath) throws ClientProtocolException, IOException {
        // 1. 创建上传需要的元素类型
        // 1.1 装载本地上传图片的文件
        File imageFile = new File(imageFilePath);
        FileBody imageFileBody = new FileBody(imageFile);

        // 2. 将所有需要上传元素打包成HttpEntity对象
        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("file", imageFileBody).build();
        System.out.println("打包数据完成");
        // 3. 创建HttpPost对象，用于包含信息发送post消息
        HttpPost httpPost = new HttpPost(path);
        httpPost.setEntity(reqEntity);
        System.out.println("创建post请求并装载好打包数据");
        // 4. 创建HttpClient对象，传入httpPost执行发送网络请求的动作
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        System.out.println("发送post请求并获取结果");
        // 5. 获取返回的实体内容对象并解析内容
        HttpEntity resultEntity = response.getEntity();
        String responseMessage = "";
        try{
            System.out.println("开始解析结果");
            if(resultEntity!=null){
                InputStream is = resultEntity.getContent();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                StringBuffer sb = new StringBuffer();
                String line = "";
                while((line = br.readLine()) != null){
                    sb.append(line);
                }
                responseMessage = sb.toString();
                System.out.println("解析完成，解析内容为"+ responseMessage);
                System.out.println("unicodeDecode"+ unicodeDecode(responseMessage));
            }
            EntityUtils.consume(resultEntity);
        }finally{
            if (null != response){
                response.close();
            }
        }
        return responseMessage;
    }

    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpSendMultipart ht = new HttpSendMultipart();
        ht.start("http://110.42.188.114:5001/prod-api/ocr","dfsdfsdfsdf","/Users/liyiruo/Desktop/orc/1.jpg");
    }


    /**
     * @Title: unicodeDecode
     * @Description: unicode解码
     * @param string
     * @return
     */
    public static String unicodeDecode(String string) {
        Pattern pattern = compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(string);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            string = string.replace(matcher.group(1), ch + "");
        }
        return string;
    }


}